/**
 * 剑指 Offer 56 - I. 数组中数字出现的次数
 * https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/
 */
const { displayBaseArray } = require('../../libs/public');

/**
 * @param {number[]} nums
 * @return {number[]}
 */
function singleNumbers(nums) {
  let xOry = 0;
  for (let i = 0; i < nums.length; i += 1) {
    xOry ^= nums[i];
  }
  let b = 1;
  while (!(xOry & b)) {
    b <<= 1;
  }
  let x = 0;
  let y = 0;
  for (let i = 0; i < nums.length; i += 1) {
    if (nums[i] & b) {
      x ^= nums[i];
    } else {
      y ^= nums[i];
    }
  }
  return [x, y];
}

console.log(displayBaseArray(singleNumbers([4, 1, 4, 6])) === '[1,6]');
console.log(displayBaseArray(singleNumbers([1, 2, 10, 4, 1, 4, 3, 3])) === '[10,2]');
